package com.hc.util;

import java.util.ArrayList;
import java.util.List;

public class Util {

	public static byte[] encode(List<Byte> list) {
		byte[] bytes = new byte[list.size()];
		for (int i = 0, len = list.size(); i < len; i++) {
			bytes[i] = list.get(i);
		}
		return bytes;
	}

	public static String toHexString(String str) {
		StringBuilder sb = new StringBuilder();
		char[] chars = str.toCharArray();
		for (int i = 0, len = chars.length; i < len; i++) {
			char c = chars[i];
			sb.append(Integer.toHexString(c)).append(",");
		}
		return sb.toString();
	}

	public static List<Byte> toByte(String val) {
		List<Byte> lists = new ArrayList<Byte>();
		String[] strs = val.split(",");
		for (int i = 0; i < strs.length; i++) {
			String str = strs[i];
			if (str == null || str.equals("")) {
				continue;
			}
			lists.add(getByte(Integer.parseInt(str, 16)));
		}
		return lists;
	}

	public static byte getByte(int c) {
		return (byte) (c & 0xff);
	}
	
	//将数字ip转换为明文ip
	public static String parseIpFromInt(long ipAddress) {
		return String.format("%d.%d.%d.%d", (ipAddress >> 24 & 0xff),
				(ipAddress >> 16 & 0xff), (ipAddress >> 8 & 0xff),
				(ipAddress & 0xff));
	}
	
	public static int getLen(String... params) {
		int size = 0;
		for (String val : params) {
			size += Util.toByte(Util.toHexString(val)).size();
		}
		return size;
	}
	
	public static long ipToNum(String ip) {
		String[] vals = ip.split("\\.");
		long num = 16777216L * Long.parseLong(vals[0]) + 
		           65536L *  Long.parseLong(vals[1]) + 
		           256l * Long.parseLong(vals[2]) + 
		           Long.parseLong(vals[3]);
	    return num;
	}
	
//	public static void main(String[] args) {
//		String[] ips = {"10.5.76.171","192.168.0.16","192.168.0.12","192.168.10.16","192.198.0.16"};
//		int i = 0;
//		for (; i < ips.length; i++) {
//			String srcIp = ips[i];
//			String desIp = parseIpFromInt(ipToNum(srcIp));
//			if (!srcIp.equals(desIp)) {
//				System.out.println("原IP :" + srcIp + "---目标IP："+desIp);
//			}
//		}
//	}

}
